<?php
	/* simple template parser which mimics the template parser in prototype.js */
    class TemplateParser {
    	var $pattern = '/(^|.|\r|\n)(#\{(.*?)\})/';
		var $string = '';
		var $data = null;
		
		function TemplateParser($string = null, $pattern = null){
			$this->setString($string);
			$this->setPattern($pattern);
		}
		
		function setString($string){
			if( $string ){
				$this->string = strval($string);
			}
		}
		
		function setPattern($patt){
			if( $patt ){
				$this->pattern = strval($patt);
			}
		}
		
		function evalTemplate($arr){
			if( is_object($arr) ){
				$arr = ((array)$arr);
			}
			$this->data = &$arr;
			$ret = preg_replace_callback($this->pattern, array($this, '_doReplace'), $this->string);
			$this->data = null;
			return $ret;
		}
		
		function _doReplace($match){
			$before = $match[1];
			if( $before == '\\' || !isset($this->data[$match[3]])){
				return $match[2];
			}
			return $before . $this->data[$match[3]];
		}
    }
?>